package com.pinyougou.sellergoods.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.pojo.TbBrandExample.Criteria;
import com.pinyougou.sellergoods.service.BrandService;
import com.pinyougou.untis.PageResult;
@Service
@Transactional
public class BrandServiceImpl implements BrandService{
	@Autowired
	private TbBrandMapper brand;
	
	public List<TbBrand> findAll(){
		
		return brand.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
	    Page<TbBrand> page=	(Page<TbBrand>) brand.selectByExample(null);
		
		return new PageResult(page.getTotal(),page.getResult());

	}
	//添加品牌
	@Override
	public void add(TbBrand brands) {
		brand.insert(brands);
	}
	//查询一个，回显数据
	@Override
	public TbBrand findOne(long id) {
		
		return brand.selectByPrimaryKey(id);
	}
	//修改
	@Override
	public void update(TbBrand bran) {
		brand.updateByPrimaryKey(bran);
		
	}
	//删除方法
	@Override
	public void delete(long[] ids) {
		
		for(long id:ids) {
			brand.deleteByPrimaryKey(id);
		}
		
	}
	//按条件模糊查询
	@Override
	public PageResult like(TbBrand bran,int pageNum,int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbBrandExample example =new TbBrandExample();
		Criteria criteria=	example.createCriteria();
		if(bran !=null) {
			if(bran.getName() !=null && bran.getName().length() >0) {
				criteria.andNameLike("%"+bran.getName()+"%");
			}
			if(bran.getFirstChar()!=null && bran.getFirstChar().length() >0) {
				criteria.andFirstCharLike("%"+bran.getFirstChar()+"%");
			}
		}
		
			Page<TbBrand> page=(Page<TbBrand>)	brand.selectByExample(example );
		return  new  PageResult(page.getTotal(),page.getResult());
	}
}

